<script setup lang="ts">
import { useRouter } from 'vue-router'
import { computed, ref, onMounted, onUnmounted } from 'vue'
import { isAuthenticated, getStoredUser, logout } from '../api/auth'
import { ElMessage } from 'element-plus'

const router = useRouter()

// 获取当前用户信息
const currentUser = computed(() => {
  return getStoredUser() || null
})

// 检查用户是否有管理员权限
const isAdmin = computed(() => {
  const user = currentUser.value
  return user && (user.role === 1 || user.role === 2)
})

// 控制用户菜单的显示状态
const showUserMenu = ref(false)

// 切换用户菜单的显示状态
const toggleUserMenu = (event: MouseEvent) => {
  event.stopPropagation() // 阻止事件冒泡
  showUserMenu.value = !showUserMenu.value
}

// 点击页面其他位置关闭用户菜单
const closeUserMenu = (event: MouseEvent) => {
  const userInfo = document.querySelector('.user-info')
  if (userInfo && !userInfo.contains(event.target as Node)) {
    showUserMenu.value = false
  }
}

// 生命周期钩子 - 添加/移除事件监听器
onMounted(() => {
  window.addEventListener('click', closeUserMenu)
})

onUnmounted(() => {
  window.removeEventListener('click', closeUserMenu)
})

// 进入聊天页面
const enterChat = () => {
  if (isAuthenticated()) {
    // 已登录，直接进入聊天
    router.push('/chat')
  } else {
    // 未登录，先去登录
    router.push('/login')
  }
}

// 处理用户注销
const handleLogout = () => {
  // 调用注销函数
  logout()
  
  // 显示成功消息
  ElMessage.success('已成功注销')
  
  // 刷新页面以更新状态
  window.location.reload()
}

// 特性列表
const features = [
  {
    title: '智能对话',
    description: '自然流畅的对话体验，理解上下文，回答准确有深度',
    icon: 'el-icon-chat-dot-round'
  },
  {
    title: '创意写作',
    description: '帮助您进行文案创作、内容编辑和灵感激发',
    icon: 'el-icon-edit'
  },
  {
    title: '代码助手',
    description: '提供编程帮助、代码优化和技术问题解答',
    icon: 'el-icon-cpu'
  },
  {
    title: '知识百科',
    description: '覆盖广泛知识领域，提供准确的信息和解释',
    icon: 'el-icon-reading'
  }
]
</script>

<template>
  <div class="home-container">
    <!-- 顶部导航 -->
    <header class="header">
      <div class="logo-container">
        <img src="../assets/buding-logo.svg" alt="布丁AI" class="logo" />
        <h1 class="app-name">布丁AI</h1>
      </div>
      
      <nav class="nav-links">
        <a href="#features" class="nav-link">功能特性</a>
        <a href="#about" class="nav-link">关于我们</a>
        <a href="#contact" class="nav-link">联系方式</a>
      </nav>
      
      <!-- 根据登录状态显示不同的内容 -->
      <div v-if="isAuthenticated()" class="user-info">
        <div class="user-avatar" @click="toggleUserMenu">
          <span>{{ currentUser?.nickname?.[0] || currentUser?.username?.[0] || 'U' }}</span>
        </div>
        <div class="user-details">
          <span class="user-name" @click="toggleUserMenu">{{ currentUser?.nickname || currentUser?.username }}</span>
          <div class="user-menu" :class="{ 'visible': showUserMenu }">
            <button class="menu-item" @click="router.push('/chat')">聊天</button>
            <button v-if="isAdmin" class="menu-item" @click="router.push('/admin')">
              后台管理
            </button>
            <button class="menu-item" @click="handleLogout">退出登录</button>
          </div>
        </div>
      </div>
      <div v-else class="auth-buttons">
        <button class="login-btn" @click="router.push('/login')">登录</button>
        <button class="signup-btn" @click="router.push('/register')">注册</button>
      </div>
    </header>
    
    <!-- 主要内容 -->
    <main>
      <!-- 英雄区 -->
      <section class="hero-section">
        <div class="hero-content">
          <h1 class="hero-title">
            <span class="gradient-text">智能AI助手</span>
            <br>随时为您解答
          </h1>
          
          <p class="hero-description">
            布丁AI是您的智能伙伴，可以帮助回答问题、创作内容、编写代码，让AI技术为您的工作和生活带来便利。
          </p>
          
          <button class="cta-button" @click="enterChat">
            <el-icon><i class="el-icon-chat-dot-round"></i></el-icon>
            开始对话
          </button>
        </div>
        
        <div class="hero-image">
          <div class="chat-preview">
            <div class="preview-header">
              <div class="preview-dot"></div>
              <div class="preview-dot"></div>
              <div class="preview-dot"></div>
            </div>
            <div class="preview-content">
              <div class="ai-message">
                <div class="message-avatar"></div>
                <div class="message-bubble">你好！我是布丁AI，有什么可以帮助你的吗？</div>
              </div>
              <div class="user-message">
                <div class="message-bubble">请介绍一下你的功能</div>
                <div class="message-avatar"></div>
              </div>
              <div class="ai-message">
                <div class="message-avatar"></div>
                <div class="message-bubble">我可以帮助回答问题、创作内容、编写代码等，使用自然语言与我交流即可。</div>
              </div>
            </div>
          </div>
        </div>
      </section>
      
      <!-- 特性区 -->
      <section id="features" class="features-section">
        <h2 class="section-title">强大功能，智能体验</h2>
        <p class="section-subtitle">布丁AI提供多种实用功能，满足您的各种需求</p>
        
        <div class="features-grid">
          <div v-for="(feature, index) in features" :key="index" class="feature-card">
            <div class="feature-icon">
              <el-icon><i :class="feature.icon"></i></el-icon>
            </div>
            <h3 class="feature-title">{{ feature.title }}</h3>
            <p class="feature-description">{{ feature.description }}</p>
          </div>
        </div>
      </section>
      
      <!-- 关于我们 -->
      <section id="about" class="about-section">
        <div class="about-content">
          <h2 class="section-title">关于布丁AI</h2>
          <p class="about-text">
            布丁AI是一款基于先进自然语言处理技术开发的智能助手，致力于为用户提供高质量的对话体验。
            我们的目标是让人工智能技术更加亲民，帮助用户更高效地工作和学习。
          </p>
          <p class="about-text">
            无论是日常问答、创意写作、代码编程还是知识探索，布丁AI都能为您提供准确、有深度的回答，
            成为您的得力助手。
          </p>
        </div>
      </section>
    </main>
    
    <!-- 页脚 -->
    <footer id="contact" class="footer">
      <div class="footer-content">
        <div class="footer-logo">
          <img src="../assets/buding-logo.svg" alt="布丁AI" class="logo" />
          <span class="footer-app-name">布丁AI</span>
        </div>
        
        <div class="footer-links">
          <div class="footer-column">
            <h3 class="footer-column-title">产品</h3>
            <a href="#" class="footer-link">功能</a>
            <a href="#" class="footer-link">价格</a>
            <a href="#" class="footer-link">API</a>
          </div>
          
          <div class="footer-column">
            <h3 class="footer-column-title">资源</h3>
            <a href="#" class="footer-link">文档</a>
            <a href="#" class="footer-link">帮助中心</a>
            <a href="#" class="footer-link">使用条款</a>
          </div>
          
          <div class="footer-column">
            <h3 class="footer-column-title">联系我们</h3>
            <a href="mailto:contact@buding-ai.com" class="footer-link">contact@buding-ai.com</a>
            <div class="social-links">
              <a href="#" class="social-link"><el-icon><i class="el-icon-position"></i></el-icon></a>
              <a href="#" class="social-link"><el-icon><i class="el-icon-chat-dot-round"></i></el-icon></a>
              <a href="#" class="social-link"><el-icon><i class="el-icon-connection"></i></el-icon></a>
            </div>
          </div>
        </div>
      </div>
      
      <div class="copyright">
        © 2025 布丁AI 版权所有
      </div>
    </footer>
  </div>
</template>

<style scoped lang="scss">
.home-container {
  min-height: 100vh;
  font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  color: #374151;
  background-color: #ffffff;
}

/* 头部导航样式 */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.5rem 6%;
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  box-shadow: 0 1px 10px rgba(99, 102, 241, 0.05);
}

.logo-container {
  display: flex;
  align-items: center;
  
  .logo {
    width: 36px;
    height: 36px;
    margin-right: 12px;
    filter: drop-shadow(0 4px 6px rgba(99, 102, 241, 0.3));
  }
  
  .app-name {
    font-size: 1.4rem;
    font-weight: 700;
    margin: 0;
    background: linear-gradient(90deg, #6366F1 0%, #8B5CF6 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    text-shadow: 0 2px 10px rgba(99, 102, 241, 0.1);
  }
}

.nav-links {
  display: flex;
  gap: 2rem;
  
  .nav-link {
    color: #4b5563;
    text-decoration: none;
    font-weight: 500;
    font-size: 1rem;
    position: relative;
    padding: 0.5rem 0;
    transition: color 0.3s;
    
    &::after {
      content: '';
      position: absolute;
      width: 100%;
      height: 2px;
      bottom: 0;
      left: 0;
      background: linear-gradient(90deg, #6366F1 0%, #8B5CF6 100%);
      transform: scaleX(0);
      transform-origin: bottom right;
      transition: transform 0.3s;
    }
    
    &:hover {
      color: #6366F1;
      
      &::after {
        transform: scaleX(1);
        transform-origin: bottom left;
      }
    }
  }
}

.auth-buttons {
  display: flex;
  gap: 1rem;
  
  .login-btn {
    border: 1px solid rgba(99, 102, 241, 0.3);
    background: transparent;
    color: #6366F1;
    padding: 0.6rem 1.2rem;
    border-radius: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    
    &:hover {
      background-color: rgba(99, 102, 241, 0.05);
      transform: translateY(-2px);
    }
  }
  
  .signup-btn {
    border: none;
    background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
    color: white;
    padding: 0.6rem 1.2rem;
    border-radius: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.2);
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 6px 16px rgba(99, 102, 241, 0.3);
    }
  }
}

/* 用户信息样式 */
.user-info {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  position: relative;
  
  .user-avatar {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: 600;
    font-size: 1.1rem;
    cursor: pointer;
    box-shadow: 0 3px 10px rgba(99, 102, 241, 0.2);
    transition: transform 0.2s;
    
    &:hover {
      transform: scale(1.05);
    }
  }
  
  .user-details {
    position: relative;
    
    .user-name {
      font-weight: 600;
      color: #374151;
      cursor: pointer;
      display: flex;
      align-items: center;
      gap: 0.4rem;
      
      &::after {
        content: '▼';
        font-size: 0.6rem;
        color: #6366F1;
      }
    }
    
    .user-menu {
      position: absolute;
      top: 100%;
      right: 0;
      margin-top: 0.5rem;
      background: white;
      border-radius: 12px;
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
      padding: 0.5rem;
      display: none;
      min-width: 150px;
      z-index: 10;
      
      &.visible {
        display: block;
        animation: fadeInMenu 0.2s ease-out forwards;
      }
      
      &::before {
        content: '';
        position: absolute;
        top: -5px;
        right: 20px;
        width: 10px;
        height: 10px;
        background: white;
        transform: rotate(45deg);
        box-shadow: -2px -2px 5px rgba(0, 0, 0, 0.04);
      }
      
      .menu-item {
        display: block;
        width: 100%;
        text-align: left;
        padding: 0.8rem 1rem;
        border: none;
        background: transparent;
        color: #4b5563;
        font-size: 0.9rem;
        border-radius: 8px;
        cursor: pointer;
        transition: all 0.2s;
        
        &:hover {
          background-color: rgba(99, 102, 241, 0.08);
          color: #6366F1;
        }
      }
    }
  }
}

@keyframes fadeInMenu {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 英雄区域样式 */
.hero-section {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: calc(100vh - 5rem);
  padding: 2rem 6%;
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: 
      radial-gradient(rgba(99, 102, 241, 0.06) 1px, transparent 1px),
      radial-gradient(rgba(99, 102, 241, 0.04) 1px, transparent 1px);
    background-size: 40px 40px, 20px 20px;
    background-position: 0 0, 20px 20px;
    z-index: -1;
  }
}

.hero-content {
  max-width: 500px;
  flex: 1;
}

.hero-title {
  font-size: 3rem;
  font-weight: 800;
  line-height: 1.2;
  margin-bottom: 1.5rem;
  
  .gradient-text {
    background: linear-gradient(90deg, #6366F1 0%, #8B5CF6 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
  }
}

.hero-description {
  font-size: 1.2rem;
  line-height: 1.6;
  color: #4b5563;
  margin-bottom: 2.5rem;
}

.cta-button {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  border: none;
  background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
  color: white;
  padding: 0.8rem 2rem;
  border-radius: 16px;
  font-weight: 600;
  font-size: 1.1rem;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 4px 14px rgba(99, 102, 241, 0.25);
  
  &:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 20px rgba(99, 102, 241, 0.4);
  }
  
  .el-icon {
    font-size: 1.2rem;
  }
}

.hero-image {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  max-width: 600px;
}

.chat-preview {
  width: 100%;
  max-width: 450px;
  background-color: white;
  border-radius: 24px;
  box-shadow: 0 10px 30px rgba(99, 102, 241, 0.1),
              0 0 0 1px rgba(99, 102, 241, 0.05);
  overflow: hidden;
  transform: perspective(1000px) rotateY(-5deg) rotateX(5deg);
  transition: all 0.3s;
  
  &:hover {
    transform: perspective(1000px) rotateY(-2deg) rotateX(2deg) translateY(-10px);
    box-shadow: 0 15px 40px rgba(99, 102, 241, 0.15),
                0 0 0 1px rgba(99, 102, 241, 0.08);
  }
}

.preview-header {
  display: flex;
  gap: 8px;
  padding: 1rem;
  background: linear-gradient(90deg, rgba(99, 102, 241, 0.1) 0%, rgba(139, 92, 246, 0.1) 100%);
  border-bottom: 1px solid rgba(99, 102, 241, 0.05);
  
  .preview-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    
    &:nth-child(1) {
      background-color: #f87171;
    }
    
    &:nth-child(2) {
      background-color: #fbbf24;
    }
    
    &:nth-child(3) {
      background-color: #34d399;
    }
  }
}

.preview-content {
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.ai-message, .user-message {
  display: flex;
  gap: 12px;
  max-width: 100%;
}

.user-message {
  justify-content: flex-end;
}

.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
}

.ai-message .message-avatar {
  background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
}

.user-message .message-avatar {
  background: linear-gradient(135deg, #60a5fa 0%, #3b82f6 100%);
}

.message-bubble {
  padding: 12px 16px;
  border-radius: 16px;
  max-width: 80%;
  font-size: 0.9rem;
  line-height: 1.5;
}

.ai-message .message-bubble {
  background-color: #f3f4f6;
  border-bottom-left-radius: 4px;
}

.user-message .message-bubble {
  background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
  color: white;
  border-bottom-right-radius: 4px;
}

/* 特性区域样式 */
.features-section {
  padding: 6rem 6%;
  text-align: center;
  background-color: #f9faff;
}

.section-title {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 1rem;
  background: linear-gradient(90deg, #6366F1 0%, #8B5CF6 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  display: inline-block;
}

.section-subtitle {
  font-size: 1.2rem;
  color: #6b7280;
  max-width: 600px;
  margin: 0 auto 4rem;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 2rem;
  max-width: 1200px;
  margin: 0 auto;
}

.feature-card {
  background-color: white;
  border-radius: 20px;
  padding: 2rem;
  box-shadow: 0 5px 20px rgba(99, 102, 241, 0.08),
              0 0 0 1px rgba(99, 102, 241, 0.03);
  transition: all 0.3s;
  
  &:hover {
    transform: translateY(-10px);
    box-shadow: 0 8px 25px rgba(99, 102, 241, 0.12),
                0 0 0 1px rgba(99, 102, 241, 0.05);
  }
}

.feature-icon {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.1) 0%, rgba(139, 92, 246, 0.1) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1.5rem;
  
  .el-icon {
    font-size: 2rem;
    color: #6366F1;
  }
}

.feature-title {
  font-size: 1.25rem;
  font-weight: 600;
  margin-bottom: 1rem;
  color: #374151;
}

.feature-description {
  color: #6b7280;
  line-height: 1.6;
}

/* 关于我们区域样式 */
.about-section {
  padding: 6rem 6%;
  background-color: white;
  display: flex;
  justify-content: center;
}

.about-content {
  max-width: 800px;
  text-align: center;
}

.about-text {
  font-size: 1.1rem;
  line-height: 1.8;
  color: #4b5563;
  margin-bottom: 1.5rem;
}

/* 页脚样式 */
.footer {
  background-color: #f3f4f6;
  padding: 4rem 6% 2rem;
}

.footer-content {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 2rem;
  margin-bottom: 3rem;
}

.footer-logo {
  display: flex;
  align-items: center;
  margin-bottom: 1rem;
  
  .logo {
    width: 30px;
    height: 30px;
    margin-right: 10px;
  }
  
  .footer-app-name {
    font-size: 1.2rem;
    font-weight: 700;
    background: linear-gradient(90deg, #6366F1 0%, #8B5CF6 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
  }
}

.footer-links {
  display: flex;
  gap: 4rem;
  flex-wrap: wrap;
}

.footer-column {
  min-width: 150px;
}

.footer-column-title {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 1.2rem;
  color: #4b5563;
}

.footer-link {
  display: block;
  color: #6b7280;
  margin-bottom: 0.8rem;
  text-decoration: none;
  transition: color 0.2s;
  
  &:hover {
    color: #6366F1;
  }
}

.social-links {
  display: flex;
  gap: 1rem;
  margin-top: 1rem;
}

.social-link {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background-color: rgba(99, 102, 241, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #6366F1;
  transition: all 0.3s;
  
  &:hover {
    background-color: #6366F1;
    color: white;
    transform: translateY(-3px);
  }
}

.copyright {
  text-align: center;
  padding-top: 2rem;
  border-top: 1px solid rgba(99, 102, 241, 0.1);
  color: #6b7280;
  font-size: 0.9rem;
}

/* 响应式适配 */
@media (max-width: 1024px) {
  .hero-section {
    flex-direction: column;
    padding-top: 3rem;
    text-align: center;
  }
  
  .hero-content {
    max-width: 100%;
    margin-bottom: 3rem;
  }
  
  .cta-button {
    margin: 0 auto;
  }
  
  .hero-title {
    font-size: 2.5rem;
  }
}

@media (max-width: 768px) {
  .header {
    padding: 1rem 4%;
  }
  
  .nav-links {
    display: none;
  }
  
  .features-grid {
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  }
  
  .footer-content {
    flex-direction: column;
    gap: 3rem;
  }
  
  .footer-links {
    width: 100%;
    justify-content: space-between;
  }
}

@media (max-width: 480px) {
  .hero-title {
    font-size: 2rem;
  }
  
  .hero-description {
    font-size: 1rem;
  }
  
  .section-title {
    font-size: 2rem;
  }
  
  .section-subtitle {
    font-size: 1rem;
  }
  
  .footer-links {
    flex-direction: column;
    gap: 2rem;
  }
}
</style> 